package com.zwf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zwf.constant.MessageConstant;
import com.zwf.constant.StatusConstant;
import com.zwf.dto.SetmealDTO;
import com.zwf.dto.SetmealPageQueryDTO;
import com.zwf.entity.Setmeal;
import com.zwf.entity.SetmealDish;
import com.zwf.exception.DeletionNotAllowedException;
import com.zwf.exception.SetmealEnableFailedException;
import com.zwf.mapper.DishMapper;
import com.zwf.mapper.SetmealDishMapper;
import com.zwf.mapper.SetmealMapper;
import com.zwf.result.PageResult;
import com.zwf.service.SetmealService;
import com.zwf.vo.DishItemVO;
import com.zwf.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zwf
 * @date 2024/2/6 21:55
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
       List<Setmeal> setmealList=setmealMapper.list(setmeal);
       return setmealList;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> queryDish(Long id) {
        List<DishItemVO> dishItemVOList=setmealDishMapper.queryBySetmealId(id);
        return dishItemVOList;
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        Long total=page.getTotal();
        List<Setmeal> result=page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 根据套餐id查询套餐及其包含的菜品
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryByIdWithDish(Long id) {
        //查詢套餐信息
        Setmeal setmeal=setmealMapper.queryById(id);
        //查詢套餐包含的所以菜品信息
        List<SetmealDish> setmealDishList=setmealDishMapper.queryDishAllBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE); //新增套餐为停售状态
        setmealMapper.add(setmeal);
        List<SetmealDish> list=setmealDTO.getSetmealDishes();
        list.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.add(list);  //套餐菜品关联表新增数据
    }

    //批量删除套餐
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id->{
            Setmeal setmeal=setmealMapper.queryById(id);
            if(setmeal.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids); //删除套餐菜品表中与其关联的数据
    }

    /**
     * 套餐起售、停售
     * @param status
     */
    @Override
    public void enAndDis(Integer status,Long id) {
        //套餐内包含未起售菜品，则套餐不能起售
        if(status==StatusConstant.ENABLE){
            setmealDishMapper.queryDishAllBySetmealId(id).forEach(setmealDish -> {
                if(dishMapper.queryById(setmealDish.getDishId()).getStatus()==StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void modify(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //修改套餐包含的菜品  逻辑：全删再添加
        List<Long> list=new ArrayList<>();
        list.add(setmealDTO.getId());
        setmealDishMapper.delete(list);
        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.add(setmealDTO.getSetmealDishes());
    }
}

